﻿/**
 * Main Class - Created by FábioTNT - FlashFrameless Team 2009
 * Project - Desso
 *
 * @author		FabioTNT - http://www.fabiotnt.com/ - FlashFrameLess Team
 * @version		1.0
 */

package com.flashframeless.gallery {

	//Import Aux Classes.
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.display.MovieClip;
    import flash.events.ContextMenuEvent;
	import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;
	import flash.ui.Mouse;
	
	import com.flashframeless.net.DiscoveryHost;
	import com.flashframeless.components.ui.lightBox.LightBox;
	
	import com.hydrotik.utils.QueueLoader;
	import com.hydrotik.utils.QueueLoaderEvent;
	
	import gs.TweenLite;
	import gs.easing.Quint;
	
	//Main script class.
	public class Main extends MovieClip {
		
		//Declare class variables.
		private var _developerURL	: String = "http://www.flashframeless.com";		
		private var _xmlGalery		: XML;
		private var _loader			: QueueLoader;
		
		public var 	gallery			: Gallery;
		public var	lightBox		: LightBox;
		
		/** 
		* Class constructor.
		*/
		public function Main():void{
						
			//Call init method.
			instanciate();
			
		}
		
		/** 
		* Init Class.
		*/
		public function Init():void{
						
			//Call init method.
			instanciate();
			
		}		
		
		private function instanciate():void {
			
			this.visible = false;
			
			//DiscoveryHost.compilerHost 	= 'http://devel.spellcaster.com.br/galeria/';
			DiscoveryHost.compilerHost 	= 'http://www.gustavoz.com.br/';
			DiscoveryHost.getActualHost();
			DiscoveryHost.mediaPath 	= 'adm';
			
			gallery 	= galleryMc;
			lightBox 	= lightBoxMc;
			
			_loader = new QueueLoader();
			
			_loader.addItem( DiscoveryHost.mediaPath + '/galeriesXML.php', null, { title:"_xmlGalery", type:'xml' } );
			
			_loader.addEventListener( QueueLoaderEvent.ITEM_COMPLETE, onLoadItemComplete, false, 0, true );
			_loader.addEventListener( QueueLoaderEvent.QUEUE_COMPLETE, onLoadQueueComplete, false, 0, true );
			
			_loader.execute();
			//setupInteractions();
			
		}
		
		/**
		 * Setup Interactions, set initials variables and call initial methods.
		 */
		private function setupInteractions():void{			
			
			//Init Thumbs
			gallery.thumbs.init();
			
			//Set Stage Proprieties
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align		= StageAlign.TOP_LEFT;
			
			//Align Objects
			onResize( null, 0 );
			
			//init onResize Listener
			stage.addEventListener( Event.RESIZE, onResize );
			
			//Call context menu setup method.
			contextMenuSetup();
			
			this.visible = true;
			
		}
		
		/**
		 * Setup context menu.
		 */
		private function contextMenuSetup():void{
			//
			//Hide default Itens.
			var menu:ContextMenu = new ContextMenu();
			menu.hideBuiltInItems();
			//
			//Set Grafikonstruct Item.
			var Developer:ContextMenuItem = new ContextMenuItem("by.FlashFrameLess Team 2009");
			Developer.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, openDeveloperSite );
			//
			//Set FullScreen Item.
			var fs:ContextMenuItem = new ContextMenuItem("Full Screen" );  
			fs.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, onShowFullScreen ); 
			//
			//Add Itens on context menu.
			menu.customItems.push( fs );
			menu.customItems.push( Developer );
			//set context menu.
			contextMenu = menu;
			
		}
		
		private function onLoadItemComplete(event:QueueLoaderEvent):void {
			
			if( event.filetype == QueueLoader.FILE_XML ) {
				
				this[event.title] = new XML( event.file );
				
			}
			
		}
		
		private function onLoadQueueComplete(event:QueueLoaderEvent):void {
			
			_loader.removeEventListener( QueueLoaderEvent.ITEM_COMPLETE, onLoadItemComplete );
			_loader.removeEventListener( QueueLoaderEvent.QUEUE_COMPLETE, onLoadQueueComplete );
			
			var arrCategorias:Array = new Array();
			
			var tempArray:Array;
			
			for ( var i:uint; i < _xmlGalery.categorias.categoria.length(); i++ ) {
				
				tempArray = [];
				
				for ( var e:uint = 0; e < _xmlGalery.categorias.categoria[i].imagem.length(); e++ ) {
					
					tempArray.push( { 
						
						id:_xmlGalery.categorias.categoria[i].imagem[e].@id,
						thumb:_xmlGalery.categorias.categoria[i].imagem[e].@thumb,
						image:_xmlGalery.categorias.categoria[i].imagem[e].@full,
						subtitle:_xmlGalery.categorias.categoria[i].imagem[e]
						
					} );
					
				}
				
				arrCategorias.push( {
					
					id: _xmlGalery.categorias.categoria[i].@id,
					/*url: _xmlGalery.categorias.categoria[i].@id,*/
					/*artist: _xmlGalery.categorias.categoria[i].artist[0],*/
					image:_xmlGalery.categorias.categoria[i].@href,
					name: _xmlGalery.categorias.categoria[i].name[0],
					clip: null,
					itens:tempArray
					
				} );
				
			}			
			
			gallery.thumbs.arrThumbs = arrCategorias;
			
			setupInteractions();
			
		}
		
		/**
		 * FullScreen Method.
		 * Set Aplication to FullScreen.
		 */
		private function onShowFullScreen( event:Event = null ):void {  
			
			stage.displayState = StageDisplayState.FULL_SCREEN;  
			
		}
		
		/**
		 * NormalScreen Method.
		 * Set Aplication to NormalScreen.
		 */
		private function onShowNormalScreen( event:Event = null ):void {  
			
			stage.displayState = StageDisplayState.NORMAL;  
			
		}
		
		/**
		 * Open Grafikonstruct Web Site.
		 */
		private function openDeveloperSite( event:ContextMenuEvent = null ):void {
			
			openURL( _developerURL );
			
		}		
		
		/**
		 * Open a URL.
		 *
		 * @param	url		String	String with a Web Address
		 * @param	window	String	Target Window ( Optional, default = _blank )
		 * @return			void
		 */
		private function openURL( url:String, window:String = "_blank" ):void {
			
			var request:URLRequest = new URLRequest( url )
			navigateToURL( request, window );
			
		}
		
		/**
		 * OnResize Handler.
		 *
		 * @param	evt		Event	Resize Event
		 * @return			void
		 */
		private function onResize( evt:Event = null, time:Number = .3 ):void {
			
			gallery.thumbs.updateSize( time );
			gallery.images.updateSize( time );		

			if( lightBox.visible ){
			
				lightBox.reAlign();
			
			}
			
		}
	}
}